package com.black.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.common.exception.database.PrimaryException;
import com.black.system.domain.SysOperLog;
import com.black.system.domain.req.monitor.query.SysOperLogQueryReq;
import com.black.system.domain.resp.monitor.SysOperLogResp;
import com.black.system.mapper.SysOperLogMapper;
import com.black.system.service.SysOperLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author L
 * @description 针对表【sys_oper_log(操作日志记录)】的数据库操作Service实现
 * @createDate 2024-08-19 10:07:23
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog>
        implements SysOperLogService {

    @Override
    public PageInfo<SysOperLogResp> paginationQuery(SysOperLogQueryReq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.paginationQuery(req);
        });
    }

    /**
     * @param req
     * @param ids
     * @return
     * @description 导出操作日志
     */
    @Override
    public List<SysOperLogResp> export(SysOperLogQueryReq req, Long[] ids) {
        List<SysOperLogResp> dataList;
        // 根据主键导出
        if (Objects.nonNull(ids) && ids.length > 0) {
            dataList = this.baseMapper.exportByIds(ids);
        } else {
            // 按查询条件导出
            dataList = this.baseMapper.paginationQuery(req);
        }
        return dataList;
    }

    /**
     * @param ids
     * @return
     * @description 删除操作日志
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Long[] ids) {
        if (Objects.isNull(ids) || ids.length == 0) {
            throw new PrimaryException();
        }

        for (Long id : ids) {
            SysOperLog sysOperLog = this.getById(id);
            if (Objects.nonNull(sysOperLog)) {
                this.removeById(id);
            }
        }
        return true;
    }
}




